package com.fenghongzhang.kuaishou.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

/**
 * 1,两三种显示模式,
 * 只有一个点,
 * 中间有数字
 *
 * 2,可以设置消息的数量
 *
 * 3,来了新消息可以刷新数量显示
 *
 *
 */
public class PointImageView extends AppCompatImageView {

    //模式
    private int displayModel = 0;
    public final static int NOT_SHOW =0;
    public final static int NOT_NUM = 1;
    public final static  int SHOW_NUM = 2;

    //画图
    private Paint paint;
    //画文字
    private TextPaint textPaint;
    //消息数量
    private int messageNum;

    public PointImageView(@NonNull Context context) {
        super(context);
        initPaint();
    }

    public PointImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public void initPaint(){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //实心
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);

        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(25);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (displayModel){
            case NOT_SHOW:
                break;
            case NOT_NUM:
                canvas.drawCircle(getWidth()-getWidth()/5,getTop()+getWidth()/5,getWidth()/5,paint);
                break;
            case SHOW_NUM:
                //getWidth + gettop 得到右上角坐标
                canvas.drawCircle(getWidth()-getWidth()/5,getTop()+getWidth()/5,getWidth()/5,paint);
//                canvas.drawCircle(getWidth()-getPaddingRight(),getPaddingTop(),getPaddingRight(),paint);
                int messageNum = getMessageNum();

                String text;
                if(messageNum > 0 && messageNum < 100){
                    text = String.valueOf(messageNum);
                }else {
                    text = "+".concat(String.valueOf(messageNum));
                }
                float len = textPaint.measureText(text);
                //根据长度,算出x,y坐标值
                float x = getWidth()-getWidth()/5 - len/2;
                float y = getTop()+getWidth()/5 + getWidth()/5/3;
                canvas.drawText(text,x,y,textPaint);
                break;
        }

    }

    public void setDisplayModel(int displayModel) {
        this.displayModel =  displayModel;
    }

    //同步刷新
    public void refreshNum(){
        invalidate();
    }

    public void refreshNumAtPost(){
        postInvalidate();
    }

    //设置显示多少条消息
    public void setMessageNum(int messageNum) {
        this.messageNum = messageNum;
    }

    public int getMessageNum() {
        return messageNum;
    }
}
